package cn.huobi.order.service.impl;

import cn.huobi.basic.config.BussinessException;
import cn.huobi.basic.service.impl.BaseServiceImpl;
import cn.huobi.basic.util.CodeGenerateUtils;
import cn.huobi.order.domain.OrderProduct;
import cn.huobi.order.dto.OrderConfirmDto;
import cn.huobi.order.mapper.OrderProductMapper;
import cn.huobi.order.service.IOrderProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huobi
 * @since 2022-11-03
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Override
    public void confirm(OrderConfirmDto orderConfirmDto) {
        // 根据id查询订单
        OrderProduct orderProduct = orderProductMapper.findById(orderConfirmDto.getId());
        // 判断订单是否存在
        if (orderProduct == null){
            throw new BussinessException("订单不存在！");
        }
        // 修改订单状态
        orderProduct.setState(1);
        orderProduct.setPrice(orderConfirmDto.getMoney());
        orderProduct.setPaySn(CodeGenerateUtils.generateOrderSn(orderProduct.getUserId()));
        orderProduct.setLastPayTime(new Date());
        orderProductMapper.update(orderProduct);
    }

    /**
     * 交易失败
     */
    @Override
    public void cancel(Long orderId) {
        // 根据id查询订单
        OrderProduct orderProduct = orderProductMapper.findById(orderId);
        // 判断订单是否存在
        if (orderProduct == null){
            throw new BussinessException("订单不存在！");
        }
        // 修改订单状态
        orderProduct.setState(-1);
        orderProductMapper.update(orderProduct);


    }
}
